package top.fzqblog.ijkplayerdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;

import java.io.IOException;

import top.fzqblog.ijkplayerdemo.R;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class MainActivity extends AppCompatActivity
        implements SurfaceHolder.Callback, IMediaPlayer.OnPreparedListener, IMediaPlayer.OnCompletionListener, IMediaPlayer.OnBufferingUpdateListener {

    IjkMediaPlayer mediaPlayer;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;

    private LinearLayout linearLayout;
    private boolean f=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView= (SurfaceView) findViewById(R.id.surfaceView);
        linearLayout= (LinearLayout) findViewById(R.id.line1);


        surfaceHolder=surfaceView.getHolder();

        surfaceHolder.addCallback(this);

        mediaPlayer=new IjkMediaPlayer();

        try {
            mediaPlayer.setDataSource("http://ofbrh1334.bkt.clouddn.com/ddr.mp4");
//            mediaPlayer.setDataSource("http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8");
            // mediaPlayer.setDataSource("rtmp://203.207.99.19:1935/live/CCTV5");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //mediaPlayer准备工作
        mediaPlayer.setOnPreparedListener(this);
        //MediaPlayer完成
        mediaPlayer.setOnCompletionListener(this);

        mediaPlayer.setOnBufferingUpdateListener(this);//当前加载进度的监听

        //当触摸surfaceview时显示或消失底部按钮
        surfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction()==(MotionEvent.ACTION_DOWN))
                {
                    if (f==true)
                    {
                        f=false;
                        linearLayout.setVisibility(View.VISIBLE);
                    }else {
                        f=true;
                        linearLayout.setVisibility(View.GONE);
                    }
                }

                return true;
            }
        });

    }



    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        //连接ijkPlayer 和surfaceHOLDER
        mediaPlayer.setDisplay(holder);
        //开启异步准备
        mediaPlayer.prepareAsync();


    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void onPrepared(IMediaPlayer iMediaPlayer) {
        iMediaPlayer.start();
    }

    @Override
    public void onCompletion(IMediaPlayer iMediaPlayer) {
        iMediaPlayer.seekTo(0);
        iMediaPlayer.start();
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {

    }

    //此处为两个按钮监听
    public void btnClick(View view) {

        switch (view.getId()) {
            case R.id.button1:
                mediaPlayer.pause();
                break;
            case R.id.button2:
                mediaPlayer.start();
                break;
        }

    }
}